﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ATM.Entities;

namespace ATM.DataAccess
{
    public class CardDA
    {
        /// <summary>
        /// Xac thuc card
        /// </summary>
        /// <param name="cardId">Ma the</param>
        /// <returns>1/0</returns>
        public int AuthenticateCard(string cardId, string cardPin)
        {
            string query = string.Format("SELECT 1 FROM DebitCard WHERE DebitCard.CardNumber = \"{0}\" AND DebitCard.CardPIN = \"{1}\"", cardId, cardPin);
            DataTable dt = new DataTable();
            dt = SqlHelper.ExcuteQuery(query);
            int result = dt.Rows.Count;
            return result == 1 ? 1 : 0;

        }

        /// <summary>
        /// Kiem tra the card
        /// </summary>
        /// <param name="cardId">Ma the</param>
        /// <returns>1/0</returns>
        public int ValidateCard(string cardId)
        {
            string query = string.Format("SELECT 1 FROM DebitCard WHERE (((DebitCard.[CardNumber])=\"{0}\"));", cardId);
            DataTable dt = new DataTable();
            dt = SqlHelper.ExcuteQuery(query);
            int result = dt.Rows.Count;
            return result == 1 ? 1 : 0;
        }
        /// <summary>
        /// Lay ve trang thai cua Card
        /// </summary>
        /// <param name="cardId">Ma the</param>
        /// <returns>1 : 0</returns>

        public int GetCardStatus(string cardId)
        {
            string query = string.Format("SELECT DebitCard.Status FROM DebitCard WHERE (((DebitCard.[CardNumber])=\"{0}\"));", cardId);
            DataTable dt = new DataTable();
            dt = SqlHelper.ExcuteQuery(query);
            int result = Convert.ToInt32(dt.Rows[0][0].ToString());
            return result;
        }

        /// <summary>
        /// Cap nhat ma PIN
        /// </summary>
        /// <param name="cardId">Ma the</param>
        /// <param name="pinCode">Ma Pin</param>
        /// <returns></returns>
        public int UpdatePinCode(string cardId, string pinCode)
        {
            try
            {
                string query = string.Format("UPDATE DebitCard SET DebitCard.CardPIN = \"{0}\" WHERE (((DebitCard.CardNumber)=\"{1}\"));", cardId);
                int resuLt = SqlHelper.ExcuteNonQuery(query);
                return resuLt;
            }
            catch(Exception ex)
            {
                throw new Exception("Process is failed");
            }
        }
    }
}
